package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.entity.PreventionHiddenRegister;
import com.ruoyi.system.entity.SysHiddenCheck;
import com.ruoyi.system.mapper.SysHiddenCheckMapper;
import com.ruoyi.system.service.PreventionHiddenRegisterService;
import com.ruoyi.system.service.SysHiddenCheckService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 安全隐患三录入(SysHiddenCheck)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 15:48:29
 */
@Service("sysHiddenCheckService")
public class SysHiddenCheckServiceImpl implements SysHiddenCheckService {
    @Resource
    private SysHiddenCheckMapper sysHiddenCheckDao;

    @Resource
    private PreventionHiddenRegisterService preventionHiddenRegisterService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysHiddenCheck queryById(Integer id) {
        return this.sysHiddenCheckDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysHiddenCheck 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysHiddenCheck> queryByPage(SysHiddenCheck sysHiddenCheck) {
        List<SysHiddenCheck> sysHiddenChecks = sysHiddenCheckDao.queryAllByLimit(sysHiddenCheck);
        return sysHiddenChecks;
    }

    /**
     * 新增数据
     *
     * @param sysHiddenCheck 实例对象
     * @return 实例对象
     */
    @Override
    public SysHiddenCheck insert(SysHiddenCheck sysHiddenCheck) {
        this.sysHiddenCheckDao.insert(sysHiddenCheck);
        // 同步到双重预防
        PreventionHiddenRegister preventionHiddenRegister = new PreventionHiddenRegister();
        preventionHiddenRegister.setHiddenCheckType(sysHiddenCheck.getCheckType());
        preventionHiddenRegister.setHiddenSource("三录入");
        preventionHiddenRegister.setHiddenCheckUser(SecurityUtils.getUsername());
        preventionHiddenRegister.setHiddenCheckTime(sysHiddenCheck.getCheckTime());
        preventionHiddenRegister.setAcceptable("是");
        preventionHiddenRegister.setCheckLevel("公司级");
        preventionHiddenRegister.setHiddenInfo(sysHiddenCheck.getHiddenInfo());
        preventionHiddenRegister.setRectificationStartTime(sysHiddenCheck.getRectificationTime());
        preventionHiddenRegister.setRectificationEndTime(sysHiddenCheck.getEndRectificationPeriod());
        preventionHiddenRegister.setHiddenLevel(sysHiddenCheck.getHiddenLevel());
        preventionHiddenRegisterService.insert(preventionHiddenRegister);
        return sysHiddenCheck;
    }

    /**
     * 修改数据
     *
     * @param sysHiddenCheck 实例对象
     * @return 实例对象
     */
    @Override
    public SysHiddenCheck update(SysHiddenCheck sysHiddenCheck) {
        this.sysHiddenCheckDao.update(sysHiddenCheck);
        return this.queryById(sysHiddenCheck.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysHiddenCheckDao.deleteById(id) > 0;
    }
}
